package co.th.helper.dao;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.apache.log4j.Logger;

import co.th.helper.common.CommonUtils;
import co.th.helper.jdbc.JdbcTemplate;
import co.th.helper.jdbc.mapper.RpRowMapper;
import co.th.helper.model.RpRating;

public class RpDAO extends JdbcTemplate<RpRating>{

	private static Logger _logger = Logger.getLogger(RpDAO.class);
	
	public List<RpRating> findAll() throws Exception{
		try {
			return findByCriteria(null);
		} catch (Exception ex) {
			_logger.error("Exception : " + ex.getMessage());
			throw new RuntimeException("Can't get all Hospital Data.");
		}
	}
	
	public RpRating findByPK(BigDecimal rpCode) throws Exception{
		try {
			RpRating rp = new RpRating();
			rp.setRpCode(rpCode);
			List<RpRating> list = findByCriteria(rp);
			if (null == list || list.isEmpty()){
				throw new Exception("Record not found.");
			}
			return list.get(0);
		} catch (Exception ex) {
			_logger.error("Exception : " + ex.getMessage());
			throw new RuntimeException("Cat not get RP Rating");
		}
	}
	
	public List<RpRating> findByCriteria(RpRating rp) throws Exception{
		try {
			StringBuffer sql = new StringBuffer("select * from rp_rating where 1=1 ");
			List<Object> paramObj = new ArrayList<Object>(0);
			
			if (null != rp){
				int index = 0;
				if (null != rp.getRpCode() && rp.getRpCode().intValue() > 0){
					sql.append(" , rp_code = ? ");
					paramObj.add(index++, rp.getRpCode());
				}
				if (!CommonUtils.chkIsnull(rp.getRpNameTH())){
					sql.append(" , rp_name_th = ? ");
					paramObj.add(index++, rp.getRpNameTH());
				}
				if (!CommonUtils.chkIsnull(rp.getRpNameEN())){
					sql.append(" , rp_name_en = ? ");
					paramObj.add(index++, rp.getRpNameEN());
				}
			}
			return this.queryList(sql.toString(), CommonUtils.convListToArray(paramObj), new RpRowMapper());
		} catch (Exception ex) {
			_logger.error("Exception : " + ex.getMessage());
			throw new RuntimeException("Can't get RP Rating");
		}
	}
	
	public synchronized RpRating insert(RpRating rp) throws Exception{
		try {
			rp.setRpCode(generatePK());
			
			final String sql = "INSERT INTO rp_rating (rp_code, rp_name_th, rp_name_en, create_by, create_date, update_by, update_date) VALUES ( ?, ?, ?, ?, ?, ?, ?)";
			Object[] params = new Object[7];
			params[0] = rp.getRpCode();
			params[1] = rp.getRpNameTH();
			params[2] = rp.getRpNameEN();
			params[3] = rp.getCreateBy();
			params[4] = rp.getCreateDate();
			params[5] = rp.getUpdateBy();
			params[6] = rp.getUpdateDate();
			
			int rowOfProcess = this.saveUnique(sql, params);
			if (rowOfProcess < 1){
				throw new Exception("Can't save RpRating data.");
			}
			return rp;
		} catch (Exception ex) {
			_logger.error("Exception : " + ex.getMessage());
			throw ex;
		}
	}
	
	public int update(RpRating rp) throws Exception{
		try {
			StringBuffer sql = new StringBuffer("UPDATE rp_rating SET ");
			List<Object> paramObj = new ArrayList<Object>(0);
			
			if (rp.getRpCode() == null || rp.getRpCode().intValue() <= 0){
				throw new Exception("No primary key.");
			}
			
			int index = 0;
			sql.append(" update_date = ?  ");
			paramObj.add(index++, new java.sql.Date(Calendar.getInstance().getTime().getTime()));
			if (!CommonUtils.chkIsnull(rp.getRpNameTH())){
				sql.append(" , rp_name_th = ? ");
				paramObj.add(index++, rp.getRpNameTH());
			}
			if (!CommonUtils.chkIsnull(rp.getRpNameEN())){
				sql.append(" , rp_name_en = ? ");
				paramObj.add(index++, rp.getRpNameEN());
			}
			
			sql.append(" WHERE rp_code = ? ");
			paramObj.add(index++, rp.getRpCode());
			
			return updateUnique(sql.toString(), CommonUtils.convListToArray(paramObj), new RpRowMapper());
		} catch (Exception ex) {
			_logger.error("Exception : " + ex.getMessage());
			throw new RuntimeException("Can't update Hospital Data.");
		}
	}
	
	public int delete(BigDecimal rpCode) throws Exception{
		try {		
			if (rpCode == null || rpCode.intValue() <= 0){
				throw new Exception("No primary key (Rp Rating Code).");
			}
			
			final String sql = "DELETE FROM rp_rating WHERE rp_code = ?";
			Object[] param = new Object[]{rpCode};
			
			return updateUnique(sql.toString(), param, new RpRowMapper());
		} catch (Exception ex) {
			_logger.error("Exception : " + ex.getMessage());
			throw new RuntimeException("Can't delete Hospital Data.");
		}
	}
	
	private BigDecimal generatePK() throws Exception{
		final String sql = "select max(rp_code) AS rp_code from rp_rating";
		_logger.debug("SQL : " + sql);
		RpRating objRow = queryUnique(sql, new Object[0], new RpRowMapper());
		return BigDecimal.valueOf(objRow.getRpCode().intValue() + 1);
	}
}
